from sqlalchemy import Column, Integer, String, Boolean, func
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.sql.schema import ForeignKey
from sqlalchemy.sql.sqltypes import DateTime

Base = declarative_base()

class Vote(Base):
    __tablename__ = "votes"
    id = Column(Integer, primary_key=True, index=True)
    question = Column(String(255), nullable=False)
    question_type = Column(String(255), nullable=False)
    created_at = Column(DateTime, server_default=func.now())
    options = relationship("VoteOption", back_populates="vote")

class VoteOption(Base):
    __tablename__ = "vote_options"
    id = Column(Integer, primary_key=True, index=True)
    vote_id = Column(Integer, ForeignKey("votes.id"), nullable=False)
    content = Column(String(255), nullable=False)
    vote_count = Column(Integer, default=0)
    vote = relationship("Vote", back_populates="options")

class UserVoteRecord(Base):
    __tablename__ = "user_vote_records"
    id = Column(Integer, primary_key=True, index=True)
    vote_id = Column(Integer, ForeignKey("votes.id"), nullable=False)
    option_ids = Column(String(255))  # JSON string of selected option IDs
    created_at = Column(DateTime, server_default=func.now())